
#include "logfile.h"

/*	firma Funciones	auxiliares	*/
void itoaLog(int32_t , char []);
void reverseLog(char []);

extern pthread_mutex_t mutex_logfile;

int32_t escribir_logfile(char* nomProceso, int32_t threadID, int32_t tipo, char* dato)
{

	FILE* logfile;
	
	char buffer[500];
	char aux_str[20];
	
	pthread_mutex_lock(&mutex_logfile);

	if ((logfile = fopen(LOG, "at")) == NULL)
	{
		if ((logfile = fopen(LOG, "wt")) == NULL)
			{
			printf("Error al abrir archivo log (abrir_logfile())\n");

			pthread_mutex_unlock(&mutex_logfile);
        	return -1;
			}
	}

	struct timeval tv;
	struct timezone tz;
	struct tm *tm;
	gettimeofday(&tv, &tz);
	tm=localtime(&tv.tv_sec);

	/*	primer forma de sacar los milisegundo	*/
	/*printf(" %d:%02d:%02d.%d \n", tm->tm_hour, tm->tm_min,
	    tm->tm_sec, (tv.tv_usec/1000));*/

	/*	otra forma de sacar los milisegundo	*/
	/*	struct timeb t_current;
	ftime (&t_current);
	printf("Tiempo =% ld% d \ n", t_current.time, t_current.millitm);*/

	/*	Hora	HH	*/
	strcpy(buffer, "[");
	if(tm->tm_hour < 10)
		strcat(buffer, "0");
	itoaLog(tm->tm_hour, aux_str);
	strcat(buffer, aux_str);
	strcat(buffer, ":");

	/*	Minuto	mm	*/
	itoaLog(tm->tm_min, aux_str);
	if(tm->tm_min < 10)
		strcat(buffer, "0");
	strcat(buffer, aux_str);
	strcat(buffer, ":");

	/*	Segundos ss	*/
	itoaLog(tm->tm_sec, aux_str);
	if(tm->tm_sec < 10)
		strcat(buffer, "0");
	strcat(buffer, aux_str);
	strcat(buffer, ".");

	/*	Milisegundos SSS */
	/*	Esta en microsegundos	*/
	itoaLog((tv.tv_usec/1000), aux_str);
	if((tv.tv_usec/1000) < 10)
		strcat(buffer, "00");
	else if((tv.tv_usec/1000) < 100)
		strcat(buffer, "0");
	strcat(buffer, aux_str);
	strcat(buffer, "] ");

	/* Nombre de Proceso */
	strcat(buffer, "["); strcat(buffer, nomProceso); strcat(buffer, "] ");

	/* ID del proceso */    
	itoaLog(getpid(), aux_str);
	strcat(buffer, "["); strcat(buffer, aux_str); strcat(buffer, "] ");

	/* Thread ID */
	itoaLog(threadID, aux_str);
	strcat(buffer, "["); strcat(buffer, aux_str); strcat(buffer, "] ");

	/* Tipo de Log */
	switch (tipo){
		case 1 : strcat(buffer,"INFO: ");
			break;
		case 2 : strcat(buffer,"WARN: ");
			break;
		case 3 : strcat(buffer,"ERROR: ");
			break;
		case 4 : strcat(buffer,"DEBUG: ");
			break;
		default: strcat(buffer,"UNKNOWN TYPE: ");
			break;
	}
    
	/* Data */
	strcat(buffer, dato); strcat(buffer, "\n");
	
	printf("%s", buffer);
	

	/* fputs(buffer, logfile); */
	fprintf(logfile, "%s", buffer);
	/* fwrite(buffer, 1, strlen(buffer), logfile); */
	/* write(logfile, buffer, strlen(buffer)); */
	
	if (fclose(logfile) != 0) {
		printf("Error cerrando log.txt.\n");
	}

	pthread_mutex_unlock(&mutex_logfile);
	return 0;

}

/* itoa:  convert n to characters in s */
void itoaLog(int32_t n, char s[])
{
	 int32_t i, sign;

     if ((sign = n) < 0)  /* record sign */
         n = -n;          /* make n positive */
     i = 0;
     do {       /* generate digits in reverse order */
         s[i++] = n % 10 + '0';   /* get next digit */
     } while ((n /= 10) > 0);     /* delete it */
     if (sign < 0)
         s[i++] = '-';
     s[i] = '\0';
     reverseLog(s);
}

/* reverse:  reverse string s in place */
 void reverseLog(char s[])
{
     int32_t i, j;
     char c;

     for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
         c = s[i];
         s[i] = s[j];
         s[j] = c;
     }
}
